package a03_哈希;

import java.util.HashSet;
import java.util.Set;

/**
 * <p>
 * a04_快乐数复习5
 * </p>
 *
 * @author flyduck
 * @since 2025/2/9
 */
public class a04_快乐数复习5 {

    public static void main(String[] args) {
        System.out.println(isHappy(19));
    }

    public static boolean isHappy(int n) {
        Set<Integer> set = new HashSet<>();
        while (true){
            n = getNext(n);
            if(set.contains(n)){
                return false;
            }
            if(n == 1){
                return true;
            }
            set.add(n);
        }
    }

    //129
    //129/10=12 -> 9
    //12/10=1 -> 2
    //1/10=0 -> 1
    //1+4+81=86
    public static int getNext(int n){
        int sum = 0;
        while (n > 0) {
            sum += (n % 10) * (n % 10);
            n = n / 10;
        }
        return sum;
    }



}
